package com.ericzong.util.ui;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

public class SystemTrayFactory
{

    private static Map<Window, SystemTrayExt> cache = new HashMap<>();

    public static SystemTrayExt createSystemTray(Window window, String image, String tooltip)
    {
        SystemTrayExt systemTrayExt;
        if (cache.containsKey(window))
        {
            systemTrayExt = cache.get(window);
        }
        else
        {
            systemTrayExt = new SystemTrayExt(window, image, tooltip);
            cache.put(window, systemTrayExt);
        }

        return systemTrayExt;
    }

    public static class SystemTrayExt
    {
        private Window window;
        private String image;
        private String tooltip;

        private TrayIcon trayIcon;

        private SystemTrayExt(Window window, String image, String tooltip)
        {
            this.window = window;
            this.image = image;
            this.tooltip = tooltip;

            init();
        }

        private void init()
        {
            if (SystemTray.isSupported())
            {
                SystemTray tray = SystemTray.getSystemTray();

                PopupMenu popupMenu = new PopupMenu();
                MenuItem restoreItem = new MenuItem("Hide/Restore"); // 隐藏/还原 \u9690\u85cf/\u8fd8\u539f
                MenuItem exitItem = new MenuItem("Exit"); // 退出 \u9000\u51fa
                restoreItem.addActionListener(new RestoreItemActionListener());
                exitItem.addActionListener(new ExitItemActionListener());
                popupMenu.add(restoreItem);
                popupMenu.addSeparator();
                popupMenu.add(exitItem);

                try
                {
                    URL imageUrl = getClass().getClassLoader().getResource(image);
                    ImageIcon ico = new ImageIcon(imageUrl);
                    trayIcon = new TrayIcon(ico.getImage(), tooltip, popupMenu);

                    // BufferedImage bufferedImage = ImageIO.read(imageUrl);
                    // trayIcon = new TrayIcon(bufferedImage, tooltip, popupMenu);

                    trayIcon.setImageAutoSize(true);
                    trayIcon.addMouseListener(new RestoreItemActionListener());
                    tray.add(trayIcon);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

        private class ExitItemActionListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                window.dispose();
                System.exit(0);
            }
        }

        private class RestoreItemActionListener extends MouseAdapter implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                showOrHide();
            }

            @Override
            public void mouseClicked(MouseEvent event)
            {
                if (event.getButton() == MouseEvent.BUTTON1)
                {
                    showOrHide();
                }
            }

            private void showOrHide()
            {
                window.setVisible(!window.isVisible());
            }
        }
    }
}
